
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="keyword">
            <div class="item">
              <span v-html="'搜索词'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.keyword"
                placeholder="请输入标题"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="pageSource">
            <div class="item">
              <span v-html="'来源页面'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                v-model="searchForm.pageSource"
                placeholder="请选择来源页面"
                size="mini"
              >
                <el-option
                  v-for="item in sourceOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item prop="channel">
            <div class="item">
              <span v-html="'来源渠道'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                v-model="searchForm.channel"
                placeholder="请选择来源渠道"
                size="mini"
              >
                <el-option
                  v-for="item in channelOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
      :height="653"
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="keyword"
          align="center"
          label="搜索词"
           
        >
        </el-table-column>
        <el-table-column
        sortable
          prop="count"
          align="center"
          label="搜索次数"
           
        >
        </el-table-column>
        <el-table-column
          prop="pageSource"
          align="center"
          label="来源页面"
           
        >
        <template slot-scope="scope">
          {{sourceFormatter[scope.row.pageSource]}}
        </template>
        </el-table-column>
        <el-table-column
          prop="channel"
          align="center"
          label="来源渠道"
           
        >
            <template slot-scope="scope">
                {{channelFormatter[scope.row.channel]}}
            </template>
        </el-table-column>
          <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              v-has="'operation:hot_search:delete'"
              @click="delClick(scope.row)"
              >删除</el-button
            >
            
             <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'operation:hot_search:edit'"
              @click="editClick(scope.row)"
              >修改次数</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-card class="footer" v-if="total > 0">
         <div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
        sourceOptions:[
            { label: "原料搜索", value: 1 },
        { label: "法规搜索", value: 2 },
        { label: "标准库搜索", value: 3 },
        
        ],
        sourceFormatter:{
             1: "原料搜索",
        2: "法规搜索",
        3: "标准库搜索",
        },
      channelOptions: [
        { label: "pc", value: 1 },
        { label: "小程序", value: 2 },
      ],
      channelFormatter: {
        1: "pc",
        2: "小程序",
      },
      searchForm: {
        keyword: "",
        pageSource: "",
        channel: "",
      },
      tableLoading: false,
      tableHeight: 50,
      tableData: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
    };
  },

  created() {
    this.getHotList();
  },
  methods: {
    editClick(row){
          this.$prompt('请填写搜索次数', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
         let data = {
          count:value,
            keyword:row.keyword,
            channel:row.channel,
            pageSource:row.pageSource
          }
          this.$api.editCount(data).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
               this.getHotList();
            }
          });
        }).catch(() => {
                
        });
    },
    delClick(row){
         this.$confirm("确定删除当前热搜词吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            keyword:row.keyword,
            channel:row.channel,
            pageSource:row.pageSource
          }
          this.$api.delHot(data).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
               this.getHotList();
            }
          });
        })
        .catch(() => {});
    },
    searchClick() {
      this.pageNum = 1;
      this.getHotList();
    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.pageNum = 1;
      this.getHotList();
    },

    /**
     * 获取用户列表
     * @param {*}
     */
    async getHotList() {
      this.tableLoading = true;
      const res = await this.$api.searchList({
        size: this.pageSize,
        page: this.pageNum,
        condition: {
          ...this.searchForm,
        },
      });
      console.log(res.data);
      this.total = res.data.total;
      this.tableData = res.data.records;
      this.tableLoading = false;
    },

    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getHotList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getHotList();
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }

}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
.content {
  height: 35vh;
  overflow-x: hidden;
}
/deep/.el-upload--picture-card {
  width: 400px;
  height: 270px;
}
/deep/.el-upload--picture-card i {
  margin-top: 128px;
}
.imgSize {
  height: 80px;
}
</style>